package com.xx.springboot.DataStructure.LeetCode.LinkedList;

/**
 * Created by xiaoxiunique on 2018-7-26.
 */
public  class addTwoNumbers {
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */



    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);

        ListNode cur = dummyHead, p = l1, q = l2;
        int carry = 0;

        while (p != null || q != null) {
            int x = p != null ? p.val : 0;
            int y = q != null ? q.val : 0;

            int sum = x + y + carry;
            carry = sum / 10;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;

            if(p != null)
                p = p.next;

            if(q != null)
                q = q.next;
        }

        if(carry > 0)
            cur.next = new ListNode(carry);

        return dummyHead;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
        }
    }
}


